//
//  UIs_CellFold_Model.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/12/10.
//

import Foundation
import UIKit

enum UIs_CellFold_CellFoldStatus {
    /// 未展开
    case fold
    /// 展开
    case unfold
}

enum UIs_CellFold_CellStyle {
    /// 没有展开/收起
    case none
    /// 有展开/收起
    case hasFold
}

class UIs_CellFold_Model {
    var title: String?
    var desc: String? {
        didSet {
            if let desc = desc, !desc.isEmpty {
                let attStr = NSMutableAttributedString(string: desc, attributes: [NSAttributedString.Key.foregroundColor : UIColor.black ,NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20)])
                _internalUnfoldAttributedString = attStr
            }
        }
    }
    
    var unfoldHeight: CGFloat {
        get {
            guard let _internalUnfoldHeight = _internalUnfoldHeight else { return 0 }
            return _internalUnfoldHeight
        }
    }
    /// 展开的
    var unfoldAttributedString: NSMutableAttributedString? {
        get {
            return _internalUnfoldAttributedString
        }
    }
    
    var foldHeight: CGFloat {
        get {
            guard let _internalFoldHeight = _internalFoldHeight else { return 0 }
            return _internalFoldHeight
        }
    }
    var foldAttributedString: NSMutableAttributedString?
    
    var style: UIs_CellFold_CellStyle {
        get {
            if let _internalS = _internalStyle {
                return _internalS
            } else {
                return getStyle()
            }
        }
    }
    /// 默认是折叠
    var foldStatus: UIs_CellFold_CellFoldStatus = .fold
    var cellHeight: CGFloat {
        get {
            if style == .none {
                if unfoldAttributedString != nil {
                    return 44 + unfoldHeight
                } else {
                    return 44
                }
            } else {
                if foldStatus == .fold {
                    return 44 + foldHeight + 50
                } else {
                    return 44 + unfoldHeight + 50
                }
            }
        }
    }
    
    private var _internalUnfoldHeight: CGFloat?
    private var _internalFoldHeight: CGFloat?
    private var _internalUnfoldAttributedString: NSMutableAttributedString?
    private var _internalStyle: UIs_CellFold_CellStyle?


}

extension UIs_CellFold_Model {
    private func getStyle() -> UIs_CellFold_CellStyle {
        if let unFoldAttStr = unfoldAttributedString {
            let tempLabel = UILabel()
            tempLabel.numberOfLines = 0
            tempLabel.attributedText = unFoldAttStr
            
            let tempSize = CGSize.init(width: 300, height: CGFloat.greatestFiniteMagnitude)
            /// 计算desc展示到label上的总高度
            let allHeight = tempLabel.textRect(forBounds: .init(origin: .zero, size: tempSize), limitedToNumberOfLines: 0).height
            /// 这里计算了，就缓存起来
            /// 为了更精准，都向上取整
            _internalUnfoldHeight = ceil(allHeight)
            
            /// 计算desc展示到label上的4行高度
            let limited4Height = tempLabel.textRect(forBounds: .init(origin: .zero, size: tempSize), limitedToNumberOfLines: 4).height
            _internalFoldHeight = ceil(limited4Height)
            
            if unfoldHeight > foldHeight {
                _internalStyle = UIs_CellFold_CellStyle.hasFold
                return .hasFold
            } else {
                _internalStyle = UIs_CellFold_CellStyle.none
                return .none
            }
        } else {
            _internalStyle = UIs_CellFold_CellStyle.none
            return .none
        }
    }
}


